import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
import fileType from '../component/file-type'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== $Lazy
const Page 				= $Lazy.load(()=>import('#tp/page/page-container'))
const TableMenu			= $Lazy.state(()=>import('../component/table-menu'))
// ===================================================================== data
// ===================================================================== component
class Index extends React.Component{
	state={
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		forms : [
			{ label: '文件名',		name: 'file_name' },
		],
	}
	query = {}
	submit = {}
	model = {}
	
	componentDidMount(){
		this.fetch()
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'doc-file-collect/index')
	cols = [
		{ title: '名称', 		dataIndex: 'file_name', 		...fileType() },
		{ title: '时间', 		dataIndex: 'created_at', 		...$fn.table.time, },
		{ title: '来源', 		dataIndex: 'source', 			width:120 },
		{ title: '所属文件夹',	dataIndex: 'folder_name', 		width:120 },
		{ title: '版本', 		dataIndex: 'version', 			width:100 },
		{ title: '类型', 		dataIndex: 'file_type', 		width:100 },
		{ title: '大小', 		dataIndex: 'file_size', 		width:100 },
	]
	
	controls = () => {	// 操作按钮
		return [
			Controls.click.call(this,'下载文件',() => {
				$http.submit(null,'doc-file-list/download',{param:{uuids:this.state.selectedKeys}}).then((data)=>{
					data.forEach(v=>window.open(v))
				})
			}, {isKeys:true}),
			Confirm.call(this, '取消收藏', 'doc-file-collect/delete', { isKeys:true, param:(keys,rows) => {
				const uuids = rows.map(v=>v.collect_id)
				return {uuids}
			}})
		]
	}
	
	render(){
		return (
			<>
				<Page title='我的收藏' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, {noReset:true}) }
					{/* 表格 */}
					{ Table.call(this,{onMenu:(e,row,index)=>this.tableMenuRef?.open(e,row)}) }
				</Page>
				{/* 右击鼠标 菜单 */}
				<TableMenu onRef={ref=>this.tableMenuRef=ref} {...this.props} onRefresh={this.fetch} />
			</>
		)
	}
}
export default Index